﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using StockTrader.DAL.IDAL;
using StockTrader.DAL.MSSQLHelper;
using StockTrader.BusinessService.DataContract;

namespace StockTrader.DAL.SQLserver
{
    public class Classification : IClassification
    {
/*
        private SqlConnection _internalConnection;
        private SqlTransaction _internalADOTransaction = null;

        public void BeginADOTransaction()
        {
            if (_internalConnection.State != System.Data.ConnectionState.Open)
                _internalConnection.Open();
            _internalADOTransaction = _internalConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
        }
        public void RollBackTransaction()
        {
            _internalADOTransaction.Rollback();
            _internalADOTransaction = null;
        }
        public void CommitADOTransaction()
        {
            _internalADOTransaction.Commit();
            _internalADOTransaction = null;
        }
        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new SqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        //SQL string
        private const string SQL_CLASSIFICATION_INSERT = "INSERT INTO CLASSIFICATION(NAME, DESCRIPTION, LANG) VALUES (@NAME, @DESCRIPTION, @LANG); SELECT ID=@@IDENTITY;";
        private const string SQL_CLASSIFICATION_UPDATE = "UPDATE CLASSIFICATION SET NAME=@NAME, DESCRIPTION=@DESCRIPTION WHERE ID=@ID";
        private const string SQL_CLASSIFICATION_DELETE = "DELETE FROM CLASSIFICATION WHERE ID=@ID";
        private const string SQL_CLASSIFICATION_GETBYLANG = "SELECT * FROM V_CLASSIFICATION WHERE LANG=@LANG";

        #region Parameters
        private const string PARM_ID = "@ID";
        private const string PARM_NAME = "@NAME";
        private const string PARM_DESCRIPTION = "@DESCRIPTION";
        private const string PARM_LANG = "@LANG";
        #endregion

        //Functions
        public int InsertClassificationSystem(ClassificationData item)
        {
            try
            {
                SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter(PARM_NAME, SqlDbType.NVarChar),
                    new SqlParameter(PARM_DESCRIPTION, SqlDbType.NText),
                    new SqlParameter(PARM_LANG, SqlDbType.Int)};
                parms[0].Value = item.Name;
                parms[1].Value = item.Description;
                parms[2].Value = item.Lang;
                return SQLHelper.ExecuteScalar(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_CLASSIFICATION_INSERT, parms);
            }
            catch
            {
                throw;
            }
        }

        public List<ClassificationUI> GetClassificationSystemByLang(int lang)
        {
            try
            {
                List<ClassificationUI> classList = new List<ClassificationUI>();
                SqlParameter parm1 = new SqlParameter(PARM_LANG, SqlDbType.Int);
                parm1.Value = lang;
                SqlDataReader rdr = SQLHelper.ExecuteReaderSingleParm(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_CLASSIFICATION_GETBYLANG, parm1);
                while (rdr.Read())
                {
                    ClassificationUI item = new ClassificationUI(Convert.ToInt32(rdr["ID"]), rdr["NAME"].ToString(), rdr["DESCRIPTION"].ToString(), Convert.ToInt32(rdr["LANG"]), rdr["LANGTEXT"].ToString());
                    classList.Add(item);
                }
                rdr.Close();
                return classList;
            }
            catch
            {
                throw;
            }
        }

        #region IClassification Members


        public void Update(ClassificationUI item)
        {
            try
            {
                SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter(PARM_NAME, SqlDbType.NVarChar),
                    new SqlParameter(PARM_DESCRIPTION, SqlDbType.NText),
                    new SqlParameter(PARM_ID, SqlDbType.Int)};
                parms[0].Value = item.Name;
                parms[1].Value = item.Description;
                parms[2].Value = item.Id;
                SQLHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_CLASSIFICATION_UPDATE, parms);
            }
            catch
            {
                throw;
            }
        }

        public void Delete(int id)
        {
            try
            {
                SqlParameter parm1 = new SqlParameter(PARM_ID, SqlDbType.Int);
                parm1.Value = id;
                SQLHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, SQL_CLASSIFICATION_DELETE, parm1);
            }
            catch
            {
                throw;
            }
        }

        #endregion
*/
    }
}
